<template>
  <div class="banner" @mouseenter="clearTime" @mouseleave="openTime">
    <ul class="carousel-body">
      <li
        class="carousel-item"
        :class="{ fade: index == imgIndex }"
        v-for="(item, index) in BannerList"
        :key="item.id"
      >
        <RouterLink to="">
          <img :src="item.imgUrl" alt="" />
        </RouterLink>
      </li>
    </ul>
    <a href="javascript:;" class="carousel-btn prev" @click="prev"
      ><i class="iconfont icon-angle-left"></i
    ></a>
    <a href="javascript:;" class="carousel-btn next" @click="next"
      ><i class="iconfont icon-angle-right"></i
    ></a>
    <div class="carousel-indicator">
      <span
        v-for="(item, index) in BannerList"
        :key="index"
        :class="{ bgc: index == imgIndex }"
      ></span>
    </div>
  </div>
</template>

<script>
export default {
  name: "Banner",
  // 轮播图数据
  props: ["BannerList"],
  data() {
    return {
      //图片的下标
      imgIndex: 0,
      // 定时器
      timer: null,
    };
  },
  mounted() {
    this.setIntervals();
  },
  methods: {
    // 初始化定时器
    setIntervals() {
      // 轮播图自动轮播
      this.timer = setInterval(() => {
        this.imgIndex++;
        if (this.imgIndex >= this.BannerList.length) {
          this.imgIndex = 0;
        }
      }, 2000);
    },
    // 滑出开启定时器
    openTime() {
      this.setIntervals();
    },
    // 滑过暂停定时器
    clearTime() {
      clearInterval(this.timer);
      this.timer = null;
    },
    // 上一张
    prev() {
      this.imgIndex--;
    },
    // 下一张
    next() {
      this.imgIndex++;
    },
  },
  // 离开页面销毁定时器
  beforeDestroy() {
    clearInterval(this.timer);
  },
  watch: {
    // 监听当前下标 一共是5条数据 下标从0-4 所以最大等于的时候  赋值为0 最小等于-1的时候赋值为4
    imgIndex: {
      handler(val) {
        if (val >= 5) {
          this.imgIndex = 0;
        }
        if (val <= -1) {
          this.imgIndex = 4;
        }
      },
      deep: true,
      immediate: true,
    },
  },
};
</script>

<style scoped lang="scss">
.bgc {
  background-color: red !important;
}
.banner {
  width: 100%;
  height: 100%;
  min-width: 300px;
  min-height: 150px;
  position: relative;
  .carousel {
    &-body {
      width: 100%;
      height: 100%;
    }
    &-item {
      width: 100%;
      height: 100%;
      position: absolute;
      left: 0;
      top: 0;
      opacity: 0;
      transition: opacity 0.5s linear;
      &.fade {
        opacity: 1;
        z-index: 1;
      }
      img {
        width: 100%;
        height: 100%;
      }
    }
    &-indicator {
      position: absolute;
      left: 0;
      bottom: 20px;
      z-index: 2;
      width: 100%;
      text-align: center;
      span {
        display: inline-block;
        width: 12px;
        height: 12px;
        background: rgba(0, 0, 0, 0.2);
        border-radius: 50%;
        cursor: pointer;
        ~ span {
          margin-left: 12px;
        }
        &.active {
          background: #fff;
        }
      }
    }
    &-btn {
      width: 44px;
      height: 44px;
      background: rgba(0, 0, 0, 0.2);
      color: #fff;
      border-radius: 50%;
      position: absolute;
      top: 228px;
      z-index: 2;
      text-align: center;
      line-height: 44px;
      opacity: 0;
      transition: all 0.5s;
      &.prev {
        left: 20px;
      }
      &.next {
        right: 20px;
      }
    }
  }
  &:hover {
    .carousel-btn {
      opacity: 1;
    }
  }
}
</style>
